//package java1;

import java.sql.*;
import oracle.jdbc.*;
import oracle.jdbc.pool.OracleDataSource;

import java.io.*;
import java.util.*;
/**
 *
 * @author woolgate
 */
public class Main {
    static int nTimes=10000;
    static String TNS="orcl";
    static String IP="127.0.0.1";
    static int mode=0;//0:thin, 1:oci;
    public static void main(String[] args) throws SQLException, IOException{
    	java.util.Date d=new java.util.Date();
        long nFirst=d.getTime();
    	System.out.println(d.getTime());
    	
    	execSQL();
        
    	d=new java.util.Date();
        double elapsedTime=(d.getTime()-nFirst)/1000.0;
        System.out.println(elapsedTime);

    }

    static void execSQL() throws SQLException, IOException{
        OracleDataSource ods=new OracleDataSource();
//        ods.setURL("jdbc:oracle:thin:eas/eas@"+TNS+":1521:orcl");
        ods.setURL("jdbc:oracle:oci:@"+TNS);
        ods.setUser("eas");
        ods.setPassword("eas");
        Connection conn=ods.getConnection();
        //conn.setAutoCommit(false);
        Statement stmt=conn.createStatement();
        stmt.execute("delete from c_cust");
        int i=0;
        while(i++<nTimes)
        {
	        if(i%100==0)
                    System.out.println(i);
                stmt.execute("INSERT INTO c_cust (cust_id) values("+i+")");
          //  if(i%3000==0)
          //  	conn.commit();
        }
        conn.commit();
        stmt.close();
        conn.close();
        ods.close();
    }

    static void querySQL() throws SQLException, IOException{
        OracleDataSource ods=new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:system/naridsa@"+IP+":1521:orcl");
//        ods.setURL("jdbc:oracle:oci:@"+TNS);
//        ods.setUser("system");
//        ods.setPassword("naridsa");
        Connection conn=ods.getConnection();
        Statement stmt=conn.createStatement();
        int i=0;
        while(i++<nTimes)
        {
	        if(i%20==0)
                    System.out.println(i+" ");
	        ResultSet rset=stmt.executeQuery("SELECT * FROM sh.products");
	        while(rset.next())
	        	;
	            //System.out.println(rset.getString(2));
	        rset.close();
	        //System.out.println();
        }
        stmt.close();
        conn.close();
        ods.close();
    }

    static void testSQL() throws SQLException, IOException{
        System.out.println("Please enter info to test conn to the db");
        String user, pswd, db;
        user= readEntry("User:");
        int slash_index=user.indexOf('/');
        if(slash_index != -1)
        {
            pswd=user.substring(slash_index +1);
            user = user.substring(0, slash_index);
        }
        else
            pswd=readEntry("pswd:");
        db=readEntry("database(a TNSName entry):");
        System.out.println("Connectting to the database...");
        System.out.flush();
        System.out.println("Connecting....");
        OracleDataSource ods=new OracleDataSource();
        ods.setURL("jdbc:oracle:oci:@"+db);
        ods.setUser(user);
        ods.setPassword(pswd);
        Connection conn= ods.getConnection();
        System.out.println("connected.");

        Statement stmt=conn.createStatement();

        ResultSet rset=stmt.executeQuery("Select * from sh.products");

        while(rset.next())
            System.out.println(rset.getString(2));
        rset.close();
        stmt.close();
        conn.close();
        System.out.println("Your JDBC installation is correct.");
    }

    static String readEntry(String prompt) {
        try {
            StringBuffer buffer = new StringBuffer();
            System.out.print(prompt);
            System.out.flush();
            int c = System.in.read();
            while (c != '\n' && c != -1) {
                buffer.append((char) c);
                c = System.in.read();
            }
            return buffer.toString().trim();
        } catch (IOException e) {
            return "";
        }
    }

    static void testVersion() throws SQLException{
        OracleDataSource ods=new OracleDataSource();
        ods.setURL("jdbc:oracle:thin:scott/tiger@"+IP+":1521:orcl");
        Connection conn=ods.getConnection();

        DatabaseMetaData meta=conn.getMetaData();
        System.out.println("JDBC driver version is "+meta.getDriverVersion() );
    }
}